from sklearn.neighbors import KNeighborsClassifier
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.metrics import accuracy_score
from sklearn.pipeline import Pipeline
from sklearn.preprocessing import StandardScaler

raw_data_X = [[3.393533211, 2.331273381],
              [3.110073483, 1.781539638],
              [1.343808831, 3.368360954],
              [3.582294042, 4.679179110],
              [2.280362439, 2.866990263],
              [7.423436942, 4.696522875],
              [5.745051997, 3.533989803],
              [9.172168622, 2.511101045],
              [7.792783481, 3.424088941],
              [7.939820817, 0.791637231],
              [7.792783481, 2.424088941],
              [7.939820817, 1.791637231],
              [7.792783481, 2.8024088941],
              [7.939820817, 4.791637231]
              ]
raw_data_y = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1,1,0]

X_train = np.array(raw_data_X)
y_train = np.array(raw_data_y)

X_train, X_test, y_train, y_test = train_test_split(X_train, y_train, test_size=0.2, random_state=666)

knn_classifier = KNeighborsClassifier(n_neighbors=3)

#管道流
nca_Pipeline=Pipeline([('scaler', StandardScaler()),('knn', knn_classifier)])
nca_Pipeline.fit(X_train, y_train)

# 预测的准确率
y_score = nca_Pipeline.score(X_test, y_test)
print(f'分数是{y_score}')

# 预测的准确率
y_predict = nca_Pipeline.predict(X_test)
print(f'预测标签是{y_predict}')
score = accuracy_score(y_test, y_predict)
print(f'分数是{score}')

#网格搜索
param_grid = [
    {
        'weights': ['uniform'],
        'n_neighbors': [i for i in range(1, 2)]
    },
    {
        'weights': ['distance'],
        'n_neighbors': [i for i in range(1, 2)],
        'p': [i for i in range(1, 2)]
    }
]

from sklearn.model_selection import GridSearchCV
'''
 gs = GridSearchCV(clf,          # 模型
                  parameters,   # 参数字典
                    n_jobs=1,     # 使用1个cpu
                  verbose=0,    # 不打印中间过程
                    cv=5)         # 5折交叉验证
'''
grid_search = GridSearchCV(knn_classifier, param_grid)
grid_search.fit(X_train, y_train)
## 最佳参数在测试集上模型分数
print("best:%f using %s" % (grid_search.best_score_,grid_search.best_params_))
